package com.onlineMIS.common;

import javax.management.RuntimeErrorException;

import org.apache.commons.lang.ObjectUtils;
import org.apache.jasper.tagplugins.jstl.core.If;
import org.omg.CORBA.UserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.onlineMIS.ORM.DAO.headQ.user.UserInforDaoImpl;
import com.onlineMIS.ORM.entity.headQ.user.RoleFunctionality;
import com.onlineMIS.ORM.entity.headQ.user.UserInfor;
import com.opensymphony.xwork2.ActionContext;

/**
 * 登录用户工具类
 * @author Administrator
 *
 */
@Repository
public class LoginUserUtils {

	
	/**
	 * 获取当前登录用户
	 * @return
	 * @throws Exception 
	 */
	public static UserInfor getLoginUser() {
		try {
			return (UserInfor) ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		} catch (Exception e) {		
			throw new RuntimeException("获取用户账户异常");
		}

	}
	
	/**
	 * 获取当前登录用户
	 * @return
	 * @throws Exception 
	 */
	public static UserInfor getChaiUser() {
		try {
			return (UserInfor) ActionContext.getContext().getSession().get(Common_util.LOGIN_CHAIN_USER);
		} catch (Exception e) {		
			throw new RuntimeException("获取用户账户异常");
		}

	}
	
	/**
	 * 获取用户名
	 * @return
	 */
	public static String getName() {
		try {
			return getLoginUser().getUser_name();
		} catch (Exception e) {		
			throw new RuntimeException("获取用户账户异常");
		}
	}
	
	
	/**
	 * 判断用户是否拥有查看成本的权限
	 * @param userInfor
	 * @return
	 */
	public  Boolean hasPermissionOnCost(UserInfor userInfor) {
		
//版本1：未修改总部权限模块前		
//		if (userInfor != null) {
//			RoleFunctionality rf = roleFunctionalityDaoImpl.getRoleFunctionality(userInfor.getRoleType(),RoleFunctionality.HEADQ_IS_SELECT_COST);
//						
//			if (rf != null) {
//				return true;
//			}else {
//				return false;
//			}
//		}
//		return false;
		
		//版本2：查询是否有权限标识
		if (userInfor != null) {
			boolean isExist = userInfor.getFunctions().contains(RoleFunctionality.SYSTEM_LOOK_COST);
			return isExist;
		}
		return false;
				
	}
	
}
